
package com.qwer.service.comm.impl;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import com.qwer.mapper.comm.UserInfoRepository;
import com.qwer.model.comm.UserInfo;
import com.qwer.service.comm.UserService;
import com.qwer.util.DateUtils;
import com.qwer.util.EncryptionHelper;

@Service
public class UserServiceImpl implements UserService{

    @Autowired
    private UserInfoRepository repository;
    
    @Override
    public Page<UserInfo> findList(Pageable pageable, UserInfo model) {
        Specification<UserInfo> spec = new Specification<UserInfo>() {

            @Override
            public Predicate toPredicate(Root<UserInfo> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                
                List<Predicate> list = new ArrayList<>();
                if (StringUtils.isNotEmpty(model.getUserName())) {
                    list.add(cb.like(root.get("userName"),"%"+model.getUserName()+"%"));
                }
                
                if (StringUtils.isNotEmpty(model.getRealName())) {
                    list.add(cb.like(root.get("realName"),"%"+model.getRealName()+"%"));
                }
                
                list.add(cb.equal(root.get("type"), "1"));
                Predicate[] p = new Predicate[list.size()];
                return cb.and(list.toArray(p));
            }
            
        };   
        Page<UserInfo> orderMasterPage = repository.findAll(spec,pageable);
        
        List<UserInfo> orderDTOList = orderMasterPage.getContent();

        return new PageImpl<UserInfo>(orderDTOList, pageable, orderMasterPage.getTotalElements());
    
    }

    @Override
    public void save(UserInfo model) {
        String pwd = new EncryptionHelper("1AA888BV9IUahhhY").encrypt(model.getPwd());
        model.setPwd(pwd);
        model.setAddTime(DateUtils.getDateTime());
        repository.save(model);
    }
   

    @Override
    public void deleteById(Integer id) {
        repository.delete(id);
        
    }

    @Override
    public UserInfo findById(Integer id) {
        UserInfo u = repository.findOne(id);
        u.setPwd(new EncryptionHelper("1AA888BV9IUahhhY").decrypt(u.getPwd()));
        return u;
    }

    @Override
    public UserInfo login(UserInfo info) {
       String pwd = new EncryptionHelper("1AA888BV9IUahhhY").encrypt(info.getPwd());
        info.setPwd(pwd);
        List<UserInfo> datas = repository.findByUserNameAndPwd(info.getUserName(),info.getPwd());
        UserInfo u= new UserInfo();
        if(!datas.isEmpty()){
            u = datas.get(0);
        }
        return u;
    }

    @Override
    public List<UserInfo> getAllUser() {
        return repository.findByType("1");
    }
    
    @Override
    public List<UserInfo> findByGroupId(Integer groupId) {
        return repository.findByGroupId(groupId);
    }

}
